<script setup lang="ts">
import { ref } from "vue"
import html2canvas from "html2canvas"
import { billListCurrent2Api } from "@/api/billv2"
import { ElLoading } from "element-plus"
import dayjs from "dayjs"
const dialog = ref(false)
const loading = ref(false)
const contentRef = ref(null)
const gridData = ref([])
const total = ref(0)
const billAmount = ref(0)
const currentDate = dayjs()
const startTime = ref(currentDate.format("YYYY年MM月DD日 HH时mm分ss秒"))

const billListCurrent2 = () => {
  billListCurrent2Api({}).then((res: any) => {
    gridData.value = res.data.list
    total.value = res.data.total
    billAmount.value = res.data.billAmount
  })
}

//截图并下载
const captureAndDownload = async () => {
  const loading = ElLoading.service({
    lock: true,
    text: "Loading",
    background: "rgba(0, 0, 0, 0.7)"
  })
  const contentDiv = contentRef.value // 获取 el-dialog 中的内容 div
  if (contentDiv) {
    try {
      const canvas = await html2canvas(contentDiv) // 使用 html2canvas 生成 Canvas 元素
      const imageData = canvas.toDataURL("image/png") // 将 Canvas 转换为图片数据

      // 创建一个下载链接并模拟点击下载
      const downloadLink = document.createElement("a")
      downloadLink.href = imageData
      downloadLink.download = "screenshot.png"
      document.body.appendChild(downloadLink)
      downloadLink.click()
      document.body.removeChild(downloadLink)
    } catch (error) {
      console.error("截图并下载失败：", error)
    }
  }
  loading.close()
}
// // 截图 复制
// const captureAndCopy = async () => {
//   const contentDiv = contentRef.value // 获取 el-dialog 中的内容 div
//   try {
//     const canvas = await html2canvas(contentDiv) // 使用 html2canvas 生成 Canvas 元素
//     const imageData = canvas.toDataURL("image/png") // 将 Canvas 转换为图片数据
//
//     // 复制图片数据到剪贴板
//     await navigator.clipboard.writeText(imageData)
//     console.log("图片已复制到剪贴板")
//   } catch (error) {
//     console.error("截图并复制失败：", error)
//   }
// }

const listShow = async (row: any) => {
  console.log(row)
  dialog.value = true
  loading.value = true
  billListCurrent2()
  loading.value = false
}
defineExpose({ listShow })
const closes = () => {
  gridData.value = []
  total.value = 0
  billAmount.value = 0
  dialog.value = false
  emit("allListClose", true)
}
const handleClose = (done: () => void) => {
  done()
  closes()
}
const emit = defineEmits(["allListClose"])
const changeCellStyle = (obj: any) => {
  if (obj.columnIndex === 1) {
    return { color: "#fff", background: "#ff7f50" } // 返回你想要的颜色样式
  }
  return {} // 如果没有特殊样式，则返回空字符串
}
</script>

<template>
  <el-dialog v-model="dialog" title="" width="800" :before-close="handleClose" destroy-on-close draggable>
    <div ref="contentRef">
      <div class="dialog-heads">
        <div class="heads-left">
          <div class="icons"><img src="@/assets/bill/tb.png" alt="" /></div>
          <div class="company-name">浙江网京</div>
        </div>
        <div class="heads-right">
          <div class="title">网京在户库存</div>
          <div class="time">库存生成时间:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ startTime }}</div>
        </div>
      </div>
      <el-table
        v-loading="loading"
        :data="gridData"
        border
        :header-cell-style="{
          background: '#fce5d3 !important',
          border: '1px solid #cccccc'
        }"
        :cell-style="changeCellStyle"
      >
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column property="acceptorName" label="承兑人" />
        <el-table-column property="billAmount" label="金额" width="100" />
        <el-table-column property="expirationDate" label="到期日期" width="100" />
        <el-table-column property="flaw" label="瑕疵" />
      </el-table>
      <div class="dialog-footers">
        <div class="dialog-footers-top">
          <div class="top-left">总数：{{ total }}</div>
          <div>总金额：{{ billAmount }} 万元</div>
        </div>
        <div class="dialog-footers-bottom">
          <div class="bottom-left">联系人：杨晓白</div>
          <div>联系电话：15869497766</div>
        </div>
      </div>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="captureAndDownload"> 截图 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss">
.dialog-heads {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  background: linear-gradient(to right, #f6b98d, #f9a169);
  padding: 20px 0;
  .heads-left {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-right: 30px;
    .icons {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      display: flex;
      font-weight: bold;
      align-items: center;
      justify-content: center;
      img {
        width: 40px;
        height: 40px;
      }
    }
    .company-name {
    }
  }
  .heads-right {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: flex-start;
    color: #fff;
    .title {
      font-size: 32px;
      font-weight: bold;
    }
    .time {
      margin-top: 10px;
    }
  }
}
.dialog-footers {
  width: 100%;
  background: linear-gradient(to right, #f6b98d, #f9a169);
  color: #fff;
  .dialog-footers-top {
    display: flex;
    padding: 20px 0;
    margin-left: 10px;
    .top-left {
      margin-right: 10px;
    }
  }
  .dialog-footers-bottom {
    display: flex;
    margin-left: 10px;
    padding: 20px 0 60px;
    .bottom-left {
      margin-right: 10px;
    }
  }
}
</style>
